<?php
namespace app\manage\service;

/**
 * 所有API类的超级父类
 * @author 李全春 <adv30@126.com> <time:>
 */
class ApiSuper{

    protected $sessionId = null;    //app接口访问者传过来的sessionId
    protected $textData  = null;    //接收到的文本数据
    protected $fileData  = null;    //接收到的文件数据
    protected $token     = '';      // token
    protected $sendTime;            //发送方发送时的时间戳
    protected $encryption       = 'off';      //是否加密 两个值 on||off,其他的值报错,on则解密对应的文本数据，返回的数据也加密
    protected $operation        = '';         //DECODE 解密   其它为加密
    protected $key              = '';         //用来解密字串的密码
    protected $expiry           = 0;          //密文从生成之时起的有效时间,单位：秒
    protected $cKeyLength       = 4;          //动态密码的长度,默认为4.可以根据传过来的时间戳动态计算

    protected $res              = [];       //执行动作之后返回的结果
    protected $status           = NULL_DATA_STATUS;        //给App返回的状态
    protected $code             = NULL_DATA_CODE;

    protected $statusInfoArr    = [
            NULL_DATA_STATUS => '没有数据返回!',
            SUCCESS_STATUS   => '返回正常数据!',
            ERR_STATUS       => '错误提示信息:'          //状态为2时再输出erroInfo信息
        ];

    protected $msg              = '';       //返回的消息字串
    protected $err              = 'noErr';  //当前错误信息提示
    protected $errInfoArr       = [
        'noErr'         => '没有找到错误!',
        'time'          => '请校对您的手机时间',
        'ver'           => '请求的接口版本不存在',
        'appkey'        => 'appkey错误',
        'module'        => 'module模块不存在',
        'controller'    => 'controller控制器不存在',
        'action'        => 'action方法不可被调用',
        'encryption'    => 'encryption加密参数错误',
        'param'         => '参数丢失',
        ];
    protected $arrData = [];
    //生成签名的常用规则
    public static function signPublic( $sendTime, $ver, $encryption, $module, $controller, $action, $token ): string
    {
        $keyA = substr( $sendTime, 0, 5 );
        $keyB = substr( $sendTime, 5, 5 );
        return md5( $ver . $keyA . $encryption . $module . $keyB . $controller . $action . $token );
    }


    // 返回运行后的数据
    public function ajaxReturn(): array
    {
        $result['status'] = $this->status;
        $result['code']   = $this->code;
        $result['msg']    = ITEM_NAME . $this->msg;
        $result['token']  = $this->token;
        $result['res']    = $this->res;

        //记录日志
        if( APP_DEBUG ){
            $logData['module']      = input('module');
            $logData['controller']  = input('controller');
            $logData['action']      = input('action');
            $logData['textData']    = input('textData', '','');
            $logData['encryption']  = input('encryption');
            $logData['sendTime']    = input('sendTime');
            $logData['expiry']      = input('expiry');
            $logData['appkey']      = input('appkey');
            $logData['resolve_val'] = serialize($this->arrData);
            $logData['result_status']      = $this->status;
            $logData['result_msg']         = $this->msg;
            $logData['result_res']         = serialize($this->res);
            // TODO 暂时先注释掉
            // $logId = module('Api_1/ApiClientLog')->addLog($logData);
        }
        return $result;
    }
}
